package wt.auto;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/11/23
 * 版权所有: admin
 */
public class EditMapperNameSpace {
    public static void main(String[] args) throws Exception {

//        delete();
        create();
    }

    public static void create() throws Exception {

        String basePath = EditMapperNameSpace.class.getResource("/").getPath().replace("/target/classes/", "").replaceFirst("/", "");
        basePath = basePath + "/src/main/java/wt/auto/mapper/";
        basePath=basePath.replaceAll("%20"," ");


        Map<String, String[]> stringMap = initMap();
        Set<Map.Entry<String, String[]>> entrySet = stringMap.entrySet();
        for (Map.Entry<String, String[]> entry : entrySet) {

            File file = new File(basePath + entry.getKey());

            if (file.exists() == false) {
                System.out.println("create不存在：" + entry.getKey()+"          "+basePath + entry.getKey());

                continue;
            }
            String content = readFile(file);

            //替换nameSpace
            int start = content.indexOf("namespace=\"") + 11;
            int end = content.indexOf("\"", start);
            String oldNamespace = content.substring(start, end);
            System.out.println(oldNamespace + ">>" + entry.getValue()[0]);

            content = content.replaceAll(oldNamespace, entry.getValue()[0]);


            //替换type
            start = content.indexOf("type=\"") + 6;
            end = content.indexOf("\"", start);
            String oldType = content.substring(start, end);
            content = content.replaceAll(oldType, entry.getValue()[1]);
            writeFile(content, file, true);
            System.out.println(oldType + ">>" + entry.getValue()[1]);

        }
    }


    public static void delete() throws Exception {

        String basePath = EditMapperNameSpace.class.getResource("/").getPath().replace("/target/classes/", "").replaceFirst("/", "");
        basePath = basePath + "/src/main/java/wt/auto/mapper/";
        basePath=basePath.replaceAll("%20"," ");

        Map<String, String[]> stringMap = initMap();
        Set<Map.Entry<String, String[]>> entrySet = stringMap.entrySet();
        for (Map.Entry<String, String[]> entry : entrySet) {

            File file = new File(basePath + entry.getKey());
            if (file.exists() == false) {
                System.out.println("delete不存在：" + entry.getKey()+"          "+basePath + entry.getKey());

                continue;
            }
            String a = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n<mapper namespace=\"*\">\r\n</mapper>";
            writeFile(a, file, false);
        }
    }


    public static Map<String, String[]> initMap() {
        Map<String, String[]> map = new HashMap();



        map.put("_AffiliateCompanyMapper.xml", new String[]{"wt.dao.dictionary.AffiliateCompanyMapper", "wt.entity.dictionary.AffiliateCompany"});

//        map.put("_CertificateTypeMapper.xml", new String[]{"wt.dao.dictionary.CertificateTypeMapper", "wt.entity.dictionary.CertificateType"});
//        map.put("_FundSetMapper.xml", new String[]{"wt.dao.dictionary.FundSetMapper", "wt.entity.dictionary.FundSet"});
//        map.put("_ItemTypeMapper.xml", new String[]{"wt.dao.dictionary.ItemTypeMapper", "wt.entity.dictionary.ItemType"});
//        map.put("_LeaveReasonMapper.xml", new String[]{"wt.dao.dictionary.LeaveReasonMapper", "wt.entity.dictionary.LeaveReason"});
//        map.put("_LockReasonMapper.xml", new String[]{"wt.dao.dictionary.LockReasonMapper", "wt.entity.dictionary.LockReason"});
//        map.put("_MajorTypeMapper.xml", new String[]{"wt.dao.dictionary.MajorTypeMapper", "wt.entity.dictionary.MajorType"});
//        map.put("_ManagerAssessTypeMapper.xml", new String[]{"wt.dao.dictionary.ManagerAssessTypeMapper", "wt.entity.dictionary.ManagerAssessType"});
//
//
//        map.put("_ExpertBaseMapper.xml", new String[]{"wt.dao.expertInfo.ExpertBaseMapper", "wt.entity.expertInfo.ExpertBase"});
//        map.put("_ExpertCertificateMapper.xml", new String[]{"wt.dao.expertInfo.ExpertCertificateMapper", "wt.entity.expertInfo.ExpertCertificate"});
//        map.put("_ExpertCompanyMapper.xml", new String[]{"wt.dao.expertInfo.ExpertCompanyMapper", "wt.entity.expertInfo.ExpertCompany"});
//        map.put("_ExpertEducationMapper.xml", new String[]{"wt.dao.expertInfo.ExpertEducationMapper", "wt.entity.expertInfo.ExpertEducation"});
//        map.put("_ExpertMajorMapper.xml", new String[]{"wt.dao.expertInfo.ExpertMajorMapper", "wt.entity.expertInfo.ExpertMajor"});
//        map.put("_ExpertReviewMapper.xml", new String[]{"wt.dao.expertInfo.ExpertReviewMapper", "wt.entity.expertInfo.ExpertReview"});
//        map.put("_ExpertTrainMapper.xml", new String[]{"wt.dao.expertInfo.ExpertTrainMapper", "wt.entity.expertInfo.ExpertTrain"});
//        map.put("_ExpertWorkMapper.xml", new String[]{"wt.dao.expertInfo.ExpertWorkMapper", "wt.entity.expertInfo.ExpertWork"});
//        map.put("_LeaderCheckMapper.xml", new String[]{"wt.dao.expertInfo.LeaderCheckMapper", "wt.entity.expertInfo.LeaderCheck"});
//        map.put("_ScheduleInfoMapper.xml", new String[]{"wt.dao.expertInfo.ScheduleInfoMapper", "wt.entity.expertInfo.ScheduleInfo"});
//        map.put("_IllegalBehaviorMapper.xml", new String[]{"wt.dao.expertInfo.IllegalBehaviorMapper", "wt.entity.expertInfo.IllegalBehavior"});
//
//
//        map.put("_AccountMapper.xml", new String[]{"wt.dao.upms.AccountMapper", "wt.entity.upms.Account"});
//        map.put("_AccountPermissionMapper.xml", new String[]{"wt.dao.upms.AccountPermissionMapper", "wt.entity.upms.AccountPermission"});
//        map.put("_AccountRoleMapper.xml", new String[]{"wt.dao.upms.AccountRoleMapper", "wt.entity.upms.AccountRole"});
//        map.put("_PermissionMapper.xml", new String[]{"wt.dao.upms.PermissionMapper", "wt.entity.upms.Permission"});
//        map.put("_PermissionRoleMapper.xml", new String[]{"wt.dao.upms.PermissionRoleMapper", "wt.entity.upms.PermissionRole"});
//        map.put("_RoleMapper.xml", new String[]{"wt.dao.upms.RoleMapper", "wt.entity.upms.Role"});
//
//
//        map.put("_TableEditLogMapper.xml", new String[]{"wt.dao.log.TableEditLogMapper", "wt.entity.log.TableEditLog"});
//        map.put("_LoginLogMapper.xml", new String[]{"wt.dao.log.LoginLogMapper", "wt.entity.log.LoginLog"});
//
//        map.put("_CompanyInfoMapper.xml", new String[]{"wt.dao.company.CompanyInfoMapper", "wt.entity.company.CompanyInfo"});
//
//        map.put("_StaffInfoMapper.xml", new String[]{"wt.dao.staff.StaffInfoMapper", "wt.entity.staff.StaffInfo"});
//
//
//        map.put("_NoticeInfoMapper.xml", new String[]{"wt.dao.notice.NoticeInfoMapper", "wt.entity.notice.NoticeInfo"});
//        map.put("_NoticeAccountMapper.xml", new String[]{"wt.dao.notice.NoticeAccountMapper", "wt.entity.notice.NoticeAccount"});
//        map.put("_NoticeRoleMapper.xml", new String[]{"wt.dao.notice.NoticeRoleMapper", "wt.entity.notice.NoticeRole"});
//
//
//
//        map.put("_ManagerAssessMapper.xml", new String[]{"wt.dao.business.ManagerAssessMapper", "wt.entity.business.ManagerAssess"});
//        map.put("_BusinessInfoMapper.xml", new String[]{"wt.dao.business.BusinessInfoMapper", "wt.entity.business.BusinessInfo"});
//        map.put("_InspectorInfoMapper.xml", new String[]{"wt.dao.business.InspectorInfoMapper", "wt.entity.business.InspectorInfo"});
//        map.put("_InspectorFundMapper.xml", new String[]{"wt.dao.business.InspectorFundMapper", "wt.entity.business.InspectorFund"});
//        map.put("_ObserverAssessMapper.xml", new String[]{"wt.dao.business.ObserverAssessMapper", "wt.entity.business.ObserverAssess"});
//        map.put("_InspectorAssessMapper.xml", new String[]{"wt.dao.business.InspectorAssessMapper", "wt.entity.business.InspectorAssess"});
//        map.put("_BusinessMajorMapper.xml", new String[]{"wt.dao.business.BusinessMajorMapper", "wt.entity.business.BusinessMajor"});
//        map.put("_ObserverReportMapper.xml", new String[]{"wt.dao.business.ObserverReportMapper", "wt.entity.business.ObserverReport"});
//        map.put("_CompanyAssessMapper.xml", new String[]{"wt.dao.business.CompanyAssessMapper", "wt.entity.business.CompanyAssess"});



        return map;
    }


    public static String readFile(File file) throws Exception {
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String lineString;
        StringBuffer buffer = new StringBuffer();
        while ((lineString = bufferedReader.readLine()) != null) {
            buffer.append(lineString);
            buffer.append("\r\n");
        }
        String content = buffer.toString();
        bufferedReader.close();
        fileReader.close();
        return content;
    }

    public static void writeFile(String content, File file, boolean isTrim) throws Exception {

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        if (isTrim) {
            content = content.trim();
        }
        fileOutputStream.write(content.getBytes());
        fileOutputStream.close();

    }

}
